package com.prp.gwt.client.widgets.mail;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.prp.gwt.client.user.mail.MailHistoryHandler;
import com.prp.gwt.client.user.mail.MailListener;

public class MailView extends VerticalPanel {
	private static MailView instance;
	public static final int INMAILBOX = 0;
	public static final int OUTMAILBOX = 1;
	public static final int NEWMAIL = 2;
	public static final int MASSMAIL = 3;
	public static final int READINMAIL = 4;
	public static final int READOUTMAIL = 5;

	
	private HTML inmailbox = new HTML("收件箱");
	private HTML outmailbox = new HTML("发件箱");
	private HTML newmail = new HTML("写信");
	private HTML massmail = new HTML("群发");
	private boolean canSendMassMail = false;
	
	private HorizontalPanel tabBar = new HorizontalPanel();
	private DeckPanel deckPanel = new DeckPanel();

	public static MailView getInstance() {
		if (instance == null)
			instance = new MailView();
		return instance;
	}

	private MailView() {
		addStyleName("topPanel");
		setHorizontalAlignment(ALIGN_CENTER);
		addTabBar();
		addDeckPanel();
	}

	private void addTabBar() {
		tabBar.addStyleName("tabBar");
		HorizontalPanel tabPanel = new HorizontalPanel();
		
		tabPanel.add(inmailbox);
		Label l1 = new Label("|");
		l1.addStyleName("tabBar-seperator");
		tabPanel.add(l1);
		
		tabPanel.add(outmailbox);
		Label l2 = new Label("|");
		l2.addStyleName("tabBar-seperator");
		tabPanel.add(l2);
		
		tabPanel.add(newmail);
		Label l3 = new Label("|");
		l3.addStyleName("tabBar-seperator");
		tabPanel.add(l3);
		
		tabPanel.add(massmail);
		//validateAuth(ClientConstants.ROOT);
		//MailListener.getInstance().canSendMassMail();

		inmailbox.setStylePrimaryName("tabBar-text");
		outmailbox.setStylePrimaryName("tabBar-text");
		newmail.setStylePrimaryName("tabBar-text");
		massmail.setStylePrimaryName("tabBar-text");
		tabBar.add(tabPanel);
		inmailbox.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				History.newItem(MailHistoryHandler.INMAILLIST);
			}
		});
		outmailbox.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				History.newItem(MailHistoryHandler.OUTMAILLIST);
			}
		});
		newmail.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				History.newItem(MailHistoryHandler.NEWMAIL);
			}

		});
		massmail.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				MailListener.getInstance().openMassMailPanel();
			}

		});
		add(tabBar);
	}

	private void addDeckPanel() {
		HorizontalPanel panel = new HorizontalPanel();
		deckPanel.add(InboxMailListPanel.getInstance());
		deckPanel.add(OutboxMailListPanel.getInstance());
		deckPanel.add(NewMailPanel.getInstance());
		deckPanel.add(MassMailPanel.getInstance());
		deckPanel.add(ReadInMailPanel.getInstance());
		deckPanel.add(ReadOutMailPanel.getInstance());
		
		panel.add(deckPanel);
		add(panel);
	}

	public void showWidget(int index) {
		if(index < 0 || index > 5)
			return;
		deckPanel.showWidget(index);
		if(index == INMAILBOX){
			inmailbox.addStyleDependentName("selected");
			outmailbox.removeStyleDependentName("selected");
			newmail.removeStyleDependentName("selected");
			massmail.removeStyleDependentName("selected");
		}
		else if(index == OUTMAILBOX){
			outmailbox.addStyleDependentName("selected");
			inmailbox.removeStyleDependentName("selected");
			newmail.removeStyleDependentName("selected");
			massmail.removeStyleDependentName("selected");
		}
		else if(index == NEWMAIL){
			newmail.addStyleDependentName("selected");
			outmailbox.removeStyleDependentName("selected");
			inmailbox.removeStyleDependentName("selected");
			massmail.removeStyleDependentName("selected");
		}else if(index == MASSMAIL){
			newmail.removeStyleDependentName("selected");
			outmailbox.removeStyleDependentName("selected");
			inmailbox.removeStyleDependentName("selected");
			massmail.addStyleDependentName("selected");
		}
	}

	public void setBarVisible(boolean visible) {
		tabBar.setVisible(visible);
	}
	
	public void enableMassMail(){
		System.out.println("invoked enableMassMail");
		canSendMassMail = true;
		System.out.println("invoked enableMassMail canSendMassMail"
				+canSendMassMail);
	}
	public void disableMassMail(){
		canSendMassMail = false;
	}
}
